package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetMealDishMapper setmealDishMapper;
    /**
     * 新增菜品和对应口味
     * @param dishDTO
     * @return
     */

    @Override
    @Transactional//事务注解
    public void saveWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();

        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表插入1条数据
        dishMapper.insert(dish);

        Long id = dish.getId();
        //向口味表插入n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page =dishMapper.pageQuery(dishPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除
     * @param ids
     */

    @Override
    @Transactional //多个数据表操作，要保证事务的一致性
    public void deleteBatch(List<Long> ids) {
        //判断菜品是否能够删除  存在起售或者套餐
        for(Long id:ids){
            Dish dish=dishMapper.getById(id);
            if(dish.getStatus()== StatusConstant.ENABLE){
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds!=null&&setmealIds.size()>0){
            throw  new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

//        for(Long id:ids){
//            dishMapper.deleteById(id);
//
//            dishFlavorMapper.deleteByDishId(id);
//        }

        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteByDishIds(ids);
    }


    /**
     * 根据id查询dish
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        DishVO dishVO = new DishVO();
        //根据id查询dish
        Dish dish = dishMapper.getById(id);

        //根据dish id 查询 dish flavor
        List<DishFlavor> dishFlavorList=  dishFlavorMapper.getBydishId(id);
        //封装返回dishvo
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    /**
     * 修改dish
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        //修改dish基本数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);

        //删除原有口味数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        Long id = dishDTO.getId();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(id));
            dishFlavorMapper.insertBatch(flavors);
        }

    }
    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */

    @Override
    public List<DishVO> getDishByCategoryId(Integer categoryId) {
        List<Dish> dishlist = dishMapper.getDishByCategoryId(categoryId);
        List<DishVO> dishVOList = new ArrayList<>();

        if (dishlist != null) {
            for (Dish dish : dishlist) {
                DishVO dishVO = new DishVO();
                BeanUtils.copyProperties(dish, dishVO); // 这里复制的是单个对象
                List<DishFlavor> dishFlavors = dishFlavorMapper.getBydishId(dish.getId());
                dishVO.setFlavors(dishFlavors);
                dishVOList.add(dishVO);
            }
        }

        System.out.println(dishVOList);
        return dishVOList;
    }

    @Override
    public void setDishStatus(Integer status, Long id) {
        dishMapper.setDishStatus(status,id);
    }

    @Override
    public List<DishVO> usergetDishByCategoryIdAndStatus(Integer categoryId) {
        List<Dish> dishlist = dishMapper.getDishByCategoryIdAndStatus(categoryId);
        List<DishVO> dishVOList = new ArrayList<>();

        if (dishlist != null) {
            for (Dish dish : dishlist) {
                DishVO dishVO = new DishVO();
                BeanUtils.copyProperties(dish, dishVO); // 这里复制的是单个对象
                List<DishFlavor> dishFlavors = dishFlavorMapper.getBydishId(dish.getId());
                dishVO.setFlavors(dishFlavors);
                dishVOList.add(dishVO);
            }
        }

        System.out.println(dishVOList);
        return dishVOList;
    }


}
